package org.ww.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ww.common.to.MemberPrice;
import org.ww.common.to.SkuFullReductionTo;
import org.ww.common.utils.PageUtils;
import org.ww.common.utils.Query;
import org.ww.coupon.dao.SkuFullReductionDao;
import org.ww.coupon.entity.MemberPriceEntity;
import org.ww.coupon.entity.SkuFullReductionEntity;
import org.ww.coupon.entity.SkuLadderEntity;
import org.ww.coupon.service.MemberPriceService;
import org.ww.coupon.service.SkuFullReductionService;
import org.ww.coupon.service.SkuLadderService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveInfo(SkuFullReductionTo skuFullReductionTo) {
        //sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuFullReductionTo,skuFullReductionEntity);
        skuFullReductionEntity.setSkuId(skuFullReductionTo.getSkuId());
        skuFullReductionEntity.setAddOther(skuFullReductionTo.getCountStatus());
        if(skuFullReductionTo.getFullPrice().compareTo(BigDecimal.ZERO) == 1){
            this.save(skuFullReductionEntity);
        }

        //sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuFullReductionTo.getSkuId());
        skuLadderEntity.setFullCount(skuFullReductionTo.getFullCount());
        skuLadderEntity.setDiscount(skuFullReductionTo.getDiscount());
        skuLadderEntity.setAddOther(skuFullReductionTo.getCountStatus());
        if(skuFullReductionTo.getFullCount() > 0){
            skuLadderService.save(skuLadderEntity);
        }

        // sms_member_price
        List<MemberPrice> memberPrice = skuFullReductionTo.getMemberPrice();

        List<MemberPriceEntity> memberPriceEntities = Optional.ofNullable(memberPrice)
                .orElseGet(ArrayList::new)
                .stream()
                .map(m -> {
                    MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                    memberPriceEntity.setSkuId(skuFullReductionTo.getSkuId());
                    memberPriceEntity.setMemberLevelId(m.getId());
                    memberPriceEntity.setMemberPrice(m.getPrice());
                    memberPriceEntity.setMemberLevelName(m.getName());
                    memberPriceEntity.setAddOther(1);
                    return memberPriceEntity;
                }).filter(m -> m.getMemberPrice().compareTo(BigDecimal.ZERO) == 1)
                .collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);

    }

}